<template>
	<div class="container add_edit">

		<div>
			<div style="margin: 20px; color: red; font-size: 20px">
				传统小程序开通流量主要求小程序累计独立访客（UV）1000以上,</div>
			<div style="margin: 20px; color: red; font-size: 20px">
				本项目基于uniAD,uv=0也可以开通流量主,支持微信官方广告和uniAD广告</div>
		</div>
		<el-form :model="form" :rules="rules" ref="ruleForm" label-width="100px">

			<el-form-item label="广告类型">
				<el-radio-group v-model="form.adType">
					<el-radio label="wxAd">微信官方</el-radio>
					<el-radio label="uniAd">Dcloud uniAD</el-radio>
				</el-radio-group>
			</el-form-item>

			<el-form-item label="首页插屏" prop="insertScreen_id" ref="insertScreen_id">
				<el-input v-model="form.insertScreen_id"></el-input>
			</el-form-item>
			<el-form-item label="首页banner" prop="home_banner" ref="home_banner">
				<el-input v-model="form.home_banner"></el-input>
			</el-form-item>
			<el-form-item label="激励视频">
				<el-tooltip :content="form.type == '禁用' ? '点击启用' : '点击禁用'" placement="top" :hide-after="1000"
					:enterable="false" effect="light">
					<el-switch v-model="form.type" active-color="#F9D749" inactive-color="#bbb" active-value="启用"
						inactive-value="禁用"></el-switch>
				</el-tooltip>
			</el-form-item>

			<el-form-item v-if="form.type=='启用'" label="激励视频" prop="excitation" ref="excitation">
				<el-input v-model="form.excitation"></el-input>
			</el-form-item>

			<el-form-item label="是否开启广告">
				<el-tooltip :content="form.state == '禁用' ? '点击启用' : '点击禁用'" placement="top" :hide-after="1000"
					:enterable="false" effect="light">
					<el-switch v-model="form.state" active-color="#F9D749" inactive-color="#bbb" active-value="启用"
						inactive-value="禁用"></el-switch>
				</el-tooltip>
			</el-form-item>

			<el-form-item>
				<el-button class="confirm_btn" @click="submitForm('ruleForm')">提 交</el-button>
			</el-form-item>
		</el-form>

	</div>
</template>

<script>
	const __name = 'ny-app-advertising';
	export default {
		data() {
			return {
				dataId: '',
				form: {
					insertScreen_id: '',
					home_banner: '',
					excitation: '',
					state: '启用',
					type: '启用',
					adType: 'uniAd'
				},
				rules: {
					insertScreen_id: [{
						required: true,
						message: '请输入文章插屏',
						trigger: 'change'
					}],
					home_banner: [{
						required: true,
						message: '请输入首页banner',
						trigger: 'change'
					}],
					excitation: [{
						required: true,
						message: '请输入文章激励视频',
						trigger: 'change'
					}]
				}
			};
		},
		methods: {
			async loadData() {
				await this.$db[__name].tofirst().then(res => {
					if (res.code == 200) {
						for (let item in this.form) {
							this.form[item] = res.datas[item];
						}
						this.dataId = res.datas._id;
					}
				});
			},
			submitForm(formName) {
				this.$refs[formName].validate((valid, obj) => {
					// 默认获取第一个未验证 form 属性名
					this.$api.set_unvalidated_form_focus(this, obj);
					if (valid) {
						if (!this.dataId) {
							this.$db[__name].add(this.form).then(res => {
								if (res.code == 200) {
									this.$message({
										message: '操作成功',
										type: 'success'
									});
									this.loadData();
								}
							}).catch(err => {
								uni.showModal({
									content: err.message,
									showCancel: false
								})
							});
						} else {
							this.$db[__name].update(this.dataId, this.form).then(res => {
								if (res.code == 200) {
									this.$message({
										message: '操作成功',
										type: 'success'
									});
									this.loadData();
								}
							}).catch(err => {
								uni.showModal({
									content: err.message,
									showCancel: false
								})
							});
						}
					}
				});
			},

		},
		created() {
			this.loadData();
		}
	};
</script>

<style></style>
